//
//  LYDiscovery.swift
//  RxSwift_Practice
//
//  Created by 李永彬 on 2020/4/28.
//  Copyright © 2020 李永彬. All rights reserved.
//

import Foundation
import CoreBluetooth

public struct LYDiscovery: Equatable {
    public static func == (lhs: LYDiscovery, rhs: LYDiscovery) -> Bool {
        guard lhs.remotePeripheral == rhs.remotePeripheral else {
            return false
        }
        return lhs.remotePeripheral == rhs.remotePeripheral
    }
    
    /// The advertised name derived from the advertisement data.
//    public var localName: String? {
//        return advertisementData?[CBAdvertisementDataLocalNameKey] as? String
//    }
    
    private var _localName: String?
    var localName: String? {
           get {
               guard _localName != nil else {
                return self.remotePeripheral.peripheral?.name
               }
               return _localName
           }
           
           set {
               _localName = newValue
           }
       }

    public let advertisementData: [String: Any]?
    /// The remote peripheral that was discovered.
    public var remotePeripheral: LYRemotePeripheral
    
    public let RSSI: Int?
    
    init(advertisementData: [String: Any]?, remotePeripheral: LYRemotePeripheral, RSSI: Int?) {
        
        self.remotePeripheral = remotePeripheral
        self.advertisementData = advertisementData
        self.RSSI = RSSI
        guard self.advertisementData != nil else {
            return
        }
                             
        if advertisementData!.keys.contains("kCBAdvDataLocalName") {
            let name: String = advertisementData?["kCBAdvDataLocalName"] as! String
            localName = name
        }
    }
}
